package com.qianfeng.zhangwei.sharepush.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.qianfeng.zhangwei.sharepush.push.MsgActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.sharepush
 * @类 名： JpushReciver
 * @创 建人： zhangwei
 * @创建时间：2015-08-10 15:59
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注： 动态注册   随时注销注册
 *
 * 静态注册   程序销毁也存在
 */


public class JpushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //获取手机用户在jpush服务器注册id
        if (intent.getAction().equals(JPushInterface.ACTION_REGISTRATION_ID)) {
            String registerid = intent.getExtras().getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.e("registerid", registerid);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            // 接受发送的消息
            String msgId = intent.getExtras().getString(JPushInterface.EXTRA_MSG_ID);
            String title = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String msgContent = intent.getExtras().getString(JPushInterface.EXTRA_ALERT);
            //将推送的小心保存到数据库
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            //获取自定义的消息
            String msgId = intent.getExtras().getString(JPushInterface.EXTRA_MSG_ID);
            String title = intent.getExtras().getString(JPushInterface.EXTRA_MESSAGE);
            String content = intent.getExtras().getString(JPushInterface.EXTRA_ALERT);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            String msgId = intent.getExtras().getString(JPushInterface.EXTRA_MSG_ID);
            String title = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String msgContent = intent.getExtras().getString(JPushInterface.EXTRA_ALERT);
            Intent startIntent = new Intent(context, MsgActivity.class);
            startIntent.putExtra("msgid", msgId);
            startIntent.putExtra("title", title);
            startIntent.putExtra("msgContent", msgContent);
            //从服务或者广播中启动Activity
            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(startIntent);
        }
    }
}
